<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="计划编号" prop="planNumber">
        <el-input
          v-model="queryParams.planNumber"
          placeholder="请输入采购计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态 " prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus"
                   placeholder="请选择状态" clearable>
          <el-option label="草稿" value="0"/>
          <el-option label="已审批" value="1"/>
          <el-option label="已完成" value="2"/>
          <el-option label="待审批" value="3"/>
          <el-option label="已驳回" value="4"/>
          <el-option label="终止" value="5"/>
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="applyPersonName">
        <el-input
          v-model="queryParams.applyPersonName"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请部门" prop="applyDepartment">
        <el-input
          v-model="queryParams.applyDepartment"
          placeholder="请输入申请部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否认领" prop="isExport">
        <el-select v-model="queryParams.isExport"
                   placeholder="请选择状态" clearable>
          <el-option label="已认领" value="Y"/>
          <el-option label="未认领" value="N"/>
        </el-select>
      </el-form-item>
      <el-form-item label="导出人" prop="exportUser">
        <el-input
          v-model="queryParams.exportUser"
          placeholder="请输入导出人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划日期" prop="params">
        <el-date-picker
          clearable
          v-model="queryParams.params"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cg:planDetailTable:export']"
          v-if="ids.length > 0"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table ref="table" border v-loading="loading" :data="tableList" @selection-change="handleSelectionChange" :row-key="getRowKeys">
      <el-table-column type="selection" width="55" :reserve-selection="true"  />
      <el-table-column label="采购计划编号" align="center" prop="planNumber"/>
      <el-table-column label="采购计划日期" align="center" prop="planDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料需求截止日期" align="center" prop="materialDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.materialDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.approvalStatus === 0">草稿</el-tag>
          <el-tag type="success" v-if="scope.row.approvalStatus === 1">已审批</el-tag>
          <el-tag type="success" v-if="scope.row.approvalStatus === 2">已完成</el-tag>
          <el-tag v-if="scope.row.approvalStatus === 3">待审批</el-tag>
          <el-tag type="danger" v-if="scope.row.approvalStatus === 4">已驳回</el-tag>
          <el-tag type="danger" v-if="scope.row.approvalStatus === 5">终止</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请人姓名" align="center" prop="applyPersonName"/>
      <el-table-column label="申请部门" align="center" prop="applyDepartment"/>
      <el-table-column label="物料编号" align="center" prop="itemCode"/>
      <el-table-column label="物料名称" align="center" prop="itemName"/>
      <el-table-column label="规格型号" align="center" prop="specification"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
      <el-table-column label="物料分类名称" align="center" prop="itemTypeName"/>
      <el-table-column label="数量" align="center" prop="quantity"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="客户名称" align="center" prop="clientName"/>
      <el-table-column label="供应商" align="center" prop="vendorName"/>
      <el-table-column label="已采购数量" align="center" prop="purchasedQuantity"/>
      <el-table-column label="已到货数量" align="center" prop="arrivedQuantity"/>
      <el-table-column label="已入库数量" align="center" prop="rkQuantity"/>
      <el-table-column label="导出人" align="center" prop="exportUser"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
  </div>
</template>

<script>
import {listTable} from "@/api/mes/cg/planDetailTable";

export default {
  name: "PlanDetailTable",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      ids:[],
      // 总条数
      total: 0,
      // 采购计划明细表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNumber: null,
        approvalStatus: null,
        applyPersonName: null,
        applyDepartment: null,
        itemCode: null,
        itemName: null,
        clientName: null,
        params: [],
        isExport:null,
        exportUser:null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购计划明细列表 */
    getList() {
      this.loading = true;
      const query = {...this.queryParams};
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      listTable(query).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      if(this.ids.length === 0){
        return;
      }
      let query={'ids':this.ids};
      (async ()=>{
        try{
          await this.download('cg/planDetailTable/export', {
            ...query
          }, `采购计划明细_${new Date().getTime()}.xlsx`)
          this.$refs.table.clearSelection();
          this.handleQuery();
        }catch (error){
          this.$modal.msgError("文件下载失败")
        }
      })();
    },
    getRowKeys(row){
      return row.planDetailId;
    },
    //多选选中事件回调
    handleSelectionChange(val){
      this.ids = val.map(item => item.planDetailId)
    },
  }
};
</script>
